var templetes=[];
var page=1;
var rows=10;
$(function(){
	//alert("fsdfsdfsd")
	templetes.push(templete.getOrderFooter0Temp());
	templetes.push(templete.getOrderFooter1Temp());
	templetes.push(templete.getOrderFooter2Temp());
	templetes.push(templete.getOrderFooter3Temp());
	//获取跳转的tab标记
	index=parseURL(window.location.search).index;
	if(index!=0&&index!=1&&index!=2&&index!=3){
		index=0;
	}
	//默认选中页面跳转的tab键
	tabs('#oderstab',index);
	$(document).ready(function(){
	    $('body,html').animate({scrollTop:0});
	});
	$(window).bind("scroll",function() {
		$("#leadingMsg").html("下拉加载...")
	    // 判断窗口的滚动条是否接近页面底部，这里的20可以自定义
	    if ($(document).scrollTop() + $(window).height() == $(document).height() ) {
	    	//没有写ajax的调用，直接触发了链接的click事件。
	       $("#leadingMsg").html("--加载数据中...--")
			// 异步加载数据
	       appendData();
	    }
	});
})
//继续加载数据
function appendData(){
	   loginInfo(function(userInfo){
	    	  $.post(getRealPath()+"/xxorder/listByStatusAndUser",
					{order_status:index,userId:userInfo.id,page:page,rows:rows},
					function(ret){
				if(ret&&ret.code==200){
					var datas=ret.data;
					var xxOrderItems=[];
					page+=1;
					if(datas.length>0){
						for(var i in datas){
							html=String.formatmodel(templete.getOrderHeader(),datas[i]);
							xxOrderItems=datas[i].xxOrderItems;
							for(var j in xxOrderItems){
								//console.log(xxOrderItems[j].thumbnail)
								if(xxOrderItems[j].thumbnail!=undefined&&xxOrderItems[j].thumbnail.indexOf(",")){
									xxOrderItems[j].thumbnail=xxOrderItems[j].thumbnail.split(",")[0];
								}
								html+=String.formatmodel(templete.getOrderBody(),xxOrderItems[j]);
							}
							html+=String.formatmodel(templetes[index],datas[i]);
							$($(".tab-con")[index]).append(html);
						}
						//内容高度没有超过屏幕
						if($(document).scrollTop() + $(window).height() <=$(document).height()){
							appendData();
						}
					}else{
						$("#leadingMsg").html("--我也是有底线的--")
					}

				}else{
					$("#leadingMsg").html("--我也是有底线的--")
				}
			},'json');
	      });

}
//tab切换
function tabs(tabId, tabNum){
	page=1;
	rows=10;
	index=tabNum;
	$(tabId + " .tab-tit a").removeClass("active");
	$(tabId + " .tab-tit a").eq(tabNum).addClass("active");
	$(tabId + " .tab-con").hide();
	$(tabId + " .tab-con").eq(tabNum).show();
	//初始化选中的tab
	initOrderDataByIndex(tabNum);
	//选中时替换index参数
	if(parseURL(window.location.search).index!=undefined&&parseURL(window.location.search).index!=tabNum){
		//replaceParamVal("index",tabNum);
		location.replace(getRealPath()+"/my/myorders.html?index="+index);
	}
}
function initOrderDataByIndex(index){
	var html="";
	loginInfo(function(userInfo){
		$.post(getRealPath()+"/xxorder/listByStatusAndUser",
				{order_status:index,userId:userInfo.id,page:page,rows:rows},
				function(ret){
				$($(".tab-con")[index]).html("");
			if(ret&&ret.code==200){
				page+=1;
				var datas=ret.data;
				var xxOrderItems=[];
				if(datas.length>0){
					for(var i in datas){
						html=String.formatmodel(templete.getOrderHeader(),datas[i]);
						xxOrderItems=datas[i].xxOrderItems;
						for(var j in xxOrderItems){
							if(xxOrderItems[j].thumbnail!=undefined&&xxOrderItems[j].thumbnail.indexOf(",")){
								xxOrderItems[j].thumbnail=xxOrderItems[j].thumbnail.split(",")[0];
							}
							html+=String.formatmodel(templete.getOrderBody(),xxOrderItems[j]);
						}
						html+=String.formatmodel(templetes[index],datas[i]);
						$($(".tab-con")[index]).append(html);
					}
					//内容高度没有超过屏幕
					if($(document).scrollTop() + $(window).height() <=$(document).height()){
						appendData();
					}
				}else{
					$($(".tab-con")[index]).html('<div id="ajax_loading" style="width:300px;margin: 10px  auto 15px;text-align:center;"><div style="text-align: center;">暂无该分类的订单!</div></div>')
					$("#leadingMsg").html("")
				}


			}else{
				$($(".tab-con")[index]).html('<div id="ajax_loading" style="width:300px;margin: 10px  auto 15px;text-align:center;"><div style="text-align: center;">查询错误!</div></div>')
				$("#leadingMsg").html("")
			}
		},'json');
	})
};
function cancelOrder(_this,sn){
	$("#iosDialog1").attr("style","opacity:1;display:block;")
	$($("#iosDialog1 a")[0]).click(function(){
		$("#iosDialog1").attr("style","opacity:0;display:none;");
		$($("#iosDialog1 a")[0]).unbind();
		$($("#iosDialog1 a")[1]).unbind();
	})
	$($("#iosDialog1 a")[1]).click(function(){
		$("#iosDialog1").attr("style","opacity:0;display:none;");
		loginInfo(function(userInfo){
			$.get(getRealPath()+"/xxorder/cancelOrder",{userId:userInfo.id,sn:sn},function(ret){
				if(ret&&ret.code==200){
					$(_this).parent().parent().parent().parent().remove();
				}
			},'json');
		})
		$($("#iosDialog1 a")[0]).unbind();
		$($("#iosDialog1 a")[1]).unbind();
	})
}
//跳转订单详情
function nowPay(_this,sn){
	//window.location.href=getRealPath()+"/orderDetail.html?sn="+sn;
	window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3a59d3c5f923218e&redirect_uri="+getRealPath()+"/my/orderDetail.html?sn="+sn+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
}

//替换指定传入参数的值,paramName为参数,replaceWith为新值
function replaceParamVal(paramName,replaceWith) {
  var oUrl = this.location.href.toString();
  var re=eval('/('+ paramName+'=)([^&]*)/gi');
  var nUrl = oUrl.replace(re,paramName+'='+replaceWith);
  this.location = nUrl;
}
//确认收货
function acceptOk(_this,sn){
	$("#loadingToast").attr("style","opacity:1;display:block;");
	loginInfo(function(userInfo){
		$.get(getRealPath()+"/xxorder/acceptOk",{userId:userInfo.id,sn:sn},function(ret){
			if(ret&&ret.code==200){
				$(_this).parent().parent().parent().parent().remove();
			}
			$("#loadingToast").attr("style","opacity:0;display:none;");
		},'json');
	})
}
//评价
function pingjia(_this,sn){
	$("#loadingToast").attr("style","opacity:1;display:block;");
	window.location.href="./pingjia.html?sn="+sn;
	//$("#loadingToast").attr("style","opacity:0;display:none;");
}

